<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style type="text/css">
        * {
            padding: 0;
            margin: 0;
            text-align: center;
        }

        #topBox {
            background: skyblue;
            position: fixed;
            top: -100px;
            left: 0;
            width: 100%;
            font-size: 50px;
            font-weight: bold;
            line-height: 100px;
        }

        p {
            height: 2000px;
            background-image: linear-gradient(#e66465, #9198e5);
        }

        #back {
            width: 100px;
            height: 100px;
            line-height: 100px;
            background-color: #abcdef;
            position: fixed;
            right: 20px;
            bottom: 50px;
            cursor: pointer;
            display: none;
        }
    </style>
</head>

<body>
    <div id="topBox">
        顶部通栏广告
    </div>
    <p></p>
    <div id="back">
        回到顶部
    </div>
</body>
<script>
    // 1、当滚动距离达到300px时显示出顶部公共
    // 2、当滚动距离达到350px是显示回到顶部
    // 3、当点击回到顶部 慢慢的滑动上去
    let topBox = document.querySelector('#topBox')
    let back = document.querySelector('#back')
    onscroll = () => {
        let top = document.documentElement.scrollTop || document.body.scrollTop
        console.log(top);
        topBox.style.top = top >= 300 ? '0' : '-100px'
        back.style.display = top >= 350 ? 'block' : 'none'
    }
    back.onclick = () => {
        timer = setInterval(() => {
            let top = document.documentElement.scrollTop || document.body.scrollTop
            document.documentElement.scrollTop = document.body.scrollTop = top - 10
            if (document.documentElement.scrollTop === 0) clearInterval(timer)
        }, 10);
    }

</script>

</html>